﻿<%@ WebHandler Language="C#" Class="ZSTestRunNoticeBillHandler" %>
using System;
using System.Web;
using BusinessRule.DTO;
using System.Web.SessionState;
using DatabaseAccess.DBUtility;
using System.Data.OracleClient;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using Newtonsoft.Json;
using BusinessRule.Business;
using BusinessRule.IBusiness;
using BusinessRule.BusinessFactory;
/// <summary>
/// add by longb 2022-06-14
/// 试车通知单相关业务
/// </summary>

public class ZSTestRunNoticeBillHandler : IHttpHandler, IRequiresSessionState
{

    CurrentUserInfo m_UserInfo;
    TestRunBillBusiness noticebill = new TestRunBillBusiness();
    uLMSCommonIBusiness m_Common = uMESBusinessFactory.instance.GetuLMSCommonObject();
    #region 页面初始化
    public void ProcessRequest(HttpContext context)
    {
        try
        {
            var action = context.Request["action"].ToString();
            m_UserInfo = (CurrentUserInfo)context.Session["CurrentUserInfo"];
            switch (action)
            {
                case "GetMfgorder": //查询订单信息
                    GetMfgorder(context);
                    break;
                case"GetDutyWorkcenter"://查询责任单位
                    GetDutyWorkcenter(context);
                    break;
                case "GetResourceDef"://查询试车台
                    GetResourceDef(context);
                    break;
                case "GetTeamEmployees"://查询班组长
                    GetTeamEmployees(context);
                    break;
                case "SaveNoticeBillData"://保存试车通知单 
                    SaveNoticeBillData(context);
                    break;
                case "SubmitNoticeBillData"://提交审核试车通知单
                    SubmitNoticeBillData(context);
                    break;
                case "DeleteNoticeBillData"://删除试车通知单
                    DeleteNoticeBillData(context);
                    break;
                case "ConfirmNoticeBillData"://审核试车通知单
                    ConfirmNoticeBillData(context);
                    break;
                case "RetrunNoticeBillData"://退回试车通知单
                    RetrunNoticeBillData(context);
                    break;
                case "RecieveNoticeBillData":
                    RecieveNoticeBillData(context);
                    break;
                case"GetNoticeBillData"://查询试车通知单
                    GetNoticeBillData(context);
                    break;
            }
        }
        catch (Exception ex)
        {
            var result = new uMESProcessResult();
            result.Result = false;
            result.Message = ex.Message;
            context.Response.ContentType = "text/html";
            context.Response.Write(JsonConvert.SerializeObject(result));
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    #endregion

    #region 方法
    /// <summary>
    /// 查询订单信息
    /// </summary>
    /// <param name="context"></param>
    protected void GetMfgorder(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        var Name=context.Request["name"].ToString();
        var para=new Dictionary<string, string>();
        para.Add("Name",Name);
        result.DBTable =noticebill.GetMfgorder(para);
        result.Result=result.DBTable.Rows.Count>0;
        result.Message = result.Result ? "" : "根据条件查询不到订单信息";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 查询责任单位
    /// </summary>
    /// <param name="context"></param>
    protected void GetDutyWorkcenter(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        var para = new Dictionary<string, string>();
            para.Add("workcenterName", "试车");
        result.DBTable = noticebill.GetDutyWorkcenter(para);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 查询试车台
    /// </summary>
    /// <param name="context"></param>
    protected void GetResourceDef(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        result.DBTable = noticebill.GetResourceDef(new Dictionary<string, string>());
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
   /// <summary>
   /// 查询班组长
   /// </summary>
   /// <param name="context"></param>
    protected void GetTeamEmployees(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        result.DBTable = noticebill.GetTeamEmployees(new Dictionary<string, string>());
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 查询生产工（组）长
    /// </summary>
    /// <param name="context"></param>
    protected void GetTeamEmployee(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        //result.DBTable = m_Common.gette
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 保存试车通知单
    /// </summary>
    /// <param name="context"></param>
    protected void SaveNoticeBillData(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        var ZSTestRunNoticeBillID = dt.Rows[0]["ZSTestRunNoticeBillID"].ToString();
        
        var strSequence = "000000" + noticebill.GetNoticeBillCount();
        var noticeBillNo = "SCT-" + DateTime.Now.ToString("yyyy") + "-"+m_UserInfo.FactoryCode+"-" + strSequence.Substring(strSequence.Length - 4);
        var para = new Dictionary<string, string>();
        para.Add("ZSTESTRUNNOTICEBILLID", string.IsNullOrWhiteSpace(ZSTestRunNoticeBillID) ? Guid.NewGuid().ToString() : ZSTestRunNoticeBillID);//唯一ID
        para.Add("ZSTESTRUNNOTICEBILLNO", noticeBillNo);
        para.Add("MFGORDERID", dt.Rows[0]["MfgorderID"].ToString());//订单
        para.Add("ASSEMBLYWORKERID", dt.Rows[0]["AssemblyWorkerID"].ToString());//生产工（组）长
        para.Add("ASSEMBLYDATE", dt.Rows[0]["AssemblyDate"].ToString());//装配完成日期
        para.Add("DUTYWORKCENTERID", dt.Rows[0]["DutyWorkcenterID"].ToString());//责任中心
        para.Add("TESTBED", dt.Rows[0]["TestBed"].ToString());//试车台
        para.Add("TESTRUNTYPE", dt.Rows[0]["TestRunType"].ToString());//试车类型
        para.Add("NOTES", dt.Rows[0]["Notes"].ToString());//备注
        para.Add("CREATEEMPLOYEEID", m_UserInfo.EmployeeID.ToString());//创建人
        para.Add("BILLTYPE","2");
        var count = string.IsNullOrWhiteSpace(ZSTestRunNoticeBillID) ? noticebill.InsertNoticeBillData(para) : noticebill.UpdateNoticeBillData(para);
        result.Result = count > 0;
        result.Message = result.Result ? "保存成功" : "保存失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 提交审核试车通知单
    /// </summary>
    /// <param name="context"></param>
    protected void SubmitNoticeBillData(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        string ZSTestRunNoticeBillID = context.Request["ids"].ToString();
        var para = new Dictionary<string, string>();
        para.Add("ZSTESTRUNNOTICEBILLID", ZSTestRunNoticeBillID);
        para.Add("STATUS", "1");
        result.Result = noticebill.UpdateNoticeBillData(para) > 0;
        result.Message = result.Result?"提交成功":"提交失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 删除试车通知单
    /// </summary>
    /// <param name="context"></param>
    protected void DeleteNoticeBillData(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        string ZSTestRunNoticeBillID = context.Request["ids"].ToString();
        var para=new Dictionary<string,string>();
        para.Add("ZSTestRunNoticeBillID", ZSTestRunNoticeBillID);
        result.Result = noticebill.DeleteNoticeBillData(para) > 0;
        result.Message =result.Result? "删除成功":"删除失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 审核试车通知单
    /// </summary>
    /// <param name="context"></param>
    protected void ConfirmNoticeBillData(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        string ZSTestRunNoticeBillID = context.Request["ids"].ToString();
        var para = new Dictionary<string, string>();
        para.Add("ZSTESTRUNNOTICEBILLID", ZSTestRunNoticeBillID);
        para.Add("AUDITEMPLOYEEID", m_UserInfo.EmployeeID.ToString());
        result.Result = noticebill.UpdateNoticeBillData(para) > 0;
        result.Message =result.Result? "审核成功":"审核失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 退回试车通知单
    /// </summary>
    /// <param name="context"></param>
    protected void RetrunNoticeBillData(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        string ZSTestRunNoticeBillID = context.Request["ids"].ToString();
        var para = new Dictionary<string, string>();
        para.Add("ZSTESTRUNNOTICEBILLID", ZSTestRunNoticeBillID);
        para.Add("STATUS", "0");
        result.Result = noticebill.UpdateNoticeBillData(para) > 0;
        result.Message =result.Result? "退回成功":"退回失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 接收试车通知单信息
    /// </summary>
    /// <param name="context"></param>
    protected void RecieveNoticeBillData(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        string ZSTestRunNoticeBillID = context.Request["ids"].ToString();
        var para = new Dictionary<string, string>();
        para.Add("ZSTESTRUNNOTICEBILLID", ZSTestRunNoticeBillID);
        para.Add("RECIEVEEMPLOYEEID", m_UserInfo.EmployeeID.ToString());
        result.Result = noticebill.UpdateNoticeBillData(para) > 0;
        result.Message =result.Result? "接收成功":"接收失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 查询试车通知单信息
    /// </summary>
    /// <param name="context"></param>
    protected void GetNoticeBillData(HttpContext context)
    {
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        string pageSize = context.Request["pageSize"].ToString();
        string pageNumber = context.Request["pageNumber"].ToString();
        var para = new Dictionary<string, string>();
        para.Add("NoticeBillNo", dt.Rows[0]["NoticeBillNo"].ToString());
        para.Add("Machine", dt.Rows[0]["Machine"].ToString());
        para.Add("MfgOrdername", dt.Rows[0]["MfgOrdername"].ToString());
        para.Add("Status", dt.Rows[0]["Status"].ToString());
        para.Add("startedate", dt.Rows[0]["startedate"].ToString());
        para.Add("enddate", dt.Rows[0]["enddate"].ToString());
        para.Add("PageSize", pageSize);
        para.Add("CurrentPageIndex", pageNumber);
        uMESProcessResult result = noticebill.GetNoticeBillData(para);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    
    #endregion
    
    
    
    
    
}